<?php

    namespace Model;

    class productModel {
        private $DB;

        public function __construct() {
            $this->DB = mysqli_connect('ppt_mysql', 'root', 'root', 'ppt');
        }

        public function addProduct($title = null, $descr = null, $price = null) {
            if ($title === null && $descr === null && $price === null) {
                return "Exeption";
            }
            if (!is_int($price)) {
                return "Price must be integer";
            }
            $data = date("Y/m/d");
            $sql = "INSERT INTO product (title, description, price,created, updated)
                            VALUES
                            ('$title', '$descr', $price, '$data', '$data')";
            $response = $this->DB->query($sql);
            $sql = "select * from product where title = '$title'";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response !== null) {
                return [
                    "product" => $response,
                    "status" => "create",
                ];
            } else {
                return [
                    "status" => "Exeption"
                ];
            }
        }

        public function getAllProduct() {
            $sql = "select * from product";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response !== null) {
                return $response;
            } else {
                return [
                    "status" => "Exeption"
                ];
            }
        }

        public function getProductById($id = null) {
            if (!is_integer($id)) {
                return [
                    "status" => "Id must be integer"
                ];
            }
            $sql = "select * from product where id = '$id'";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response !== null) {
                return [
                    "product" => $response,
                    "status" => "Fiend"
                ];
            } else {
                return [
                    "status" => "Not found"
                ];
            }
        }
    }